//author:kakake
({
    TableModel: {
        "model": {
            "Id": "",
            "BookName": "",
            "BuyPrice": "",
            "BuyDate": "yyyy-MM-dd",
            "language": "0",
            "memo": "",
            "Flag": "",
            "WorkId": 0
        },
        "tablename": "Books",
        "key": "Id",
        "val": "0",
        "objval": {},
        "query": {
            "BookName": "like"
        },
        "where": "",
        "pageinfo": {},
        "WorkId": 1
    },
    //实现增删改查示例API控制器
    main: function (funname) {
        var self = this;
        self.TableModel.WorkId = LobsterAPI.ProgramData('workid'); //机构ID

        LobsterAPI.TrackLog("book", 'begin');

        //查询
        if (funname == "book.getdata") {
            self.data.resultdata = self.getdata();
        }
        //新增修改
        else if (funname == "book.savemodel") {
            self.data.resultdata.data = self.savemodel();
        }
        //删除
        else if (funname == "book.deletemodel") {
            self.data.resultdata.data = self.deletemodel();
        }

        LobsterAPI.TrackLog("book", 'end');

        return self.data.resultdata;
    },
    data: {
        loginright: LobsterAPI.ProgramData('loginright'),
        dbConn: LobsterAPI.ProgramData('dbConn'),
        resultdata: {}
    },
    getdata: function () {

        var self = this;
        var pageInfo = {};
        pageInfo.key = "Id";
        pageInfo.page = LobsterAPI.RequestQuery("page", 1);
        pageInfo.perPage = LobsterAPI.RequestQuery("limit", 10);
        pageInfo.totalRecord = 100;

        self.TableModel.where = LobsterAPI.GetWhere(self.TableModel.query);
        self.TableModel.pageinfo = pageInfo;

        LobsterAPI.TrackLog("where", self.TableModel.where);
        LobsterAPI.TrackLog("WorkId", self.TableModel.WorkId);

        var datagrid = {};
        datagrid.data = LobsterAPI.GetListModelPage(self.data.dbConn, self.TableModel);
        datagrid.count = pageInfo.totalRecord;

        return datagrid;
    },
    savemodel: function () {
        var self = this;
        self.TableModel.objval = JSON.parse(Program.data.requestBody);

        if (self.existbookname(self.TableModel.objval.BookName)) {
            throw '请不要添加重复的书籍名称';
        }

        LobsterAPI.TrackLog("TableModel", self.TableModel);
        LobsterAPI.SaveModel(self.data.dbConn, self.TableModel);
        return { result: true };
    },
    deletemodel: function () {
        var self = this;
        self.TableModel.val = LobsterAPI.RequestQuery("Id", 0);
        LobsterAPI.DeleteModel(self.data.dbConn, self.TableModel);
        return { result: true };
    },
    existbookname: function (bookname) {
        var self = this;
        var strsql = "select count(1) ct from Books where bookname=?"
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(bookname);
        var res = LobsterAPI.DbGetJsonObject(self.data.dbConn, strsql, dbparams);
        if (res.ct > 0) {
            return true;
        } else {
            return false;
        }
    }
})